package com.rocketmq;


import com.alibaba.fastjson.JSONObject;
import com.rocketmq.entities.Message;
import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.FileSystems;
import java.nio.file.Files;

/**
 * Created by 歪大哥😁 on 2019-11-12.
 */
public class Test {

    private static final String COMMIT_LOG_FILE_HOME_PATH = "/Users/kathy/studyCode/common/src/main/java/com/rocketmq/file/commitlog/";

    private static final String CONSUME_QUEUE_FILE_HOME_PATH = "/Users/kathy/studyCode/common/src/main/java/com/rocketmq/file/consumequeue/";

    public static void main(String[] args) {
//        testWriteFile();
//        testReadFile();
        testParseFile();
    }


    private static void testWriteFile() {
        String filePath = COMMIT_LOG_FILE_HOME_PATH + "0000.log";
        File file = new File(filePath);
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "rw")) {
            FileChannel fileChannel = accessFile.getChannel();
            byte[] m1 = Message.encodeMessage(new Message("helloWorld1"));
            byte[] m2 = Message.encodeMessage(new Message("asddaw"));
            byte[] m3 = Message.encodeMessage(new Message("消息333"));
            ByteBuffer buf = ByteBuffer.allocate(m1.length + m2.length + m3.length);
            buf.put(m1);
            buf.put(m2);
            buf.put(m3);
            buf.flip();
            fileChannel.write(buf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void testReadFile() {
        String filePath = COMMIT_LOG_FILE_HOME_PATH + "0000.log";
        File file = new File(filePath);
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "rw")) {
            FileChannel fileChannel = accessFile.getChannel();
            long size = fileChannel.size();
            ByteBuffer buf = ByteBuffer.allocate((int)size);
            fileChannel.read(buf);

            int messageOffset = 0;
            buf.flip();
            while (messageOffset < size) {
                int messageSize = buf.getInt(messageOffset);
                byte[] message = new byte[messageSize];
                buf.get(message, 0, messageSize);
                Message m = Message.decodeMessage(message);
                System.out.println(JSONObject.toJSONString(m));
                messageOffset += messageSize;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static String received = "socket received";
    static String send = "socket send";

    private static void testParseFile() {
        String inPath = "/Users/kathy/Downloads/http_request.log";
        String outPath = "/Users/kathy/Downloads/http_request_new.log";
        File in = getOrCreateFile(inPath);
        File out = getOrCreateFile(outPath);

        int readLine = 0;
        int writeLine = 0;
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(in))) {
            String line;
            try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(out))) {
                StringBuilder sb = new StringBuilder();
                boolean notFirst = false;
                while ((line = bufferedReader.readLine()) != null) {
                    readLine++;
                    if (StringUtils.isBlank(line)) {
                        break;
                    }
                    if (needNewLine(line) && notFirst) {
                        // 输出原有字符串
                        bufferedWriter.write(sb.toString());
                        // 换行
                        bufferedWriter.newLine();
                        // 重置sb
                        sb.setLength(0);
                        // 输出计数
                        writeLine++;
                    }
                    notFirst = true;
                    // 拼接当前行内容
                    sb.append(line.trim());
                }

                if (sb.length() > 0) {
                    // 输出最后一行数据
                    bufferedWriter.write(sb.toString());
                }
            }
        } catch (Exception e) {
            return;
        } finally {
            System.out.println("read=" + readLine);
            System.out.println("write=" + writeLine);

        }
    }

    static boolean needNewLine(String line) {
        return line.contains(received) || line.contains(send);
    }


    static File getOrCreateFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                Files.createFile(FileSystems.getDefault().getPath(filePath));
            } catch (IOException e) {
                return null;
            }
        }
        return file;
    }





}
